import json

# 字符串转list
from common.structure.linked_list import LinkedListMap, Node


def str_to_list(info: str):
    if not info:
        return ['']
    if isinstance(info, dict):
        return info
    elif isinstance(info, list):
        return info
    try:
        return eval(info)
    except Exception as e:
        return [info]


def dict_to_list(data_dict: dict):
    res_list = []
    for key in data_dict.keys():
        res_list.append(data_dict[key])
    return res_list


def dict_kv_to_list_dict(data_dict: dict):
    res_list = []
    for key in data_dict.keys():
        res_list.append({'k': key, 'v': data_dict[key]})
    return res_list


def list_dict_to_list(data_list: list, key):
    res_list = []
    for info in data_list:
        res_list.append(info[key])
    return res_list


def list_to_dict(data_list, key):
    data_dict = {}
    for info in data_list:
        data_dict[info[key]] = info
    return data_dict


def list_to_arr(data_list, title_list):
    arr = [['序号'] + title_list]
    count = 0
    for info in data_list:
        count += 1
        row = [count]
        for title in title_list:
            row.append(info.get(title))
        arr.append(row)
    return arr


def list_dict_to_arr(data_dict_list):
    total = 0
    title_dict = {}
    title_dict_reverse = {}
    for data_dict in data_dict_list:
        for key in data_dict.keys():
            if key not in title_dict.keys():
                total += 1
                title_dict[key] = total
                title_dict_reverse[total] = key
    arr = [[]]
    for i in range(total):
        arr[0].append(title_dict_reverse.get(i + 1))
    for data_dict in data_dict_list:
        row = []
        for i in range(total):
            row.append(data_dict.get(title_dict_reverse.get(i + 1)))
        arr.append(row)
    return arr


def p_float(data):
    if not data:
        return 0
    if type(data) is not float:
        if data == '-':
            return 0
        return float(data)
    return data


def p_dict(data):
    if type(data) is str:
        return json.loads(data.replace('\'', '\\"'))
    return data


def byte_to_str(byte_info):
    if byte_info:
        return str(byte_info, encoding="utf-8")
    else:
        return ''


def flow_to_stock_day_info(s_info, flow, date_info):
    return {'code': s_info.get('code'), 'name': s_info['name'], 'rq': date_info,
            'main_i': flow.main_i, 'main_o': flow.main_o,
            'main_io': flow.main_io, 'sb_i': flow.sb_i, 'sb_o': flow.sb_o, 'sb_io': flow.sb_io,
            'b_i': flow.b_i, 'b_o': flow.b_o, 'b_io': flow.b_io, 'm_i': flow.m_i,
            'm_o': flow.m_o, 'm_io': flow.m_io, 's_i': flow.s_i, 's_o': flow.s_o,
            's_io': flow.s_io, 'turnover_rate': flow.turnover_rate, 'market_value': flow.market_value,
            'circulation_value': flow.circulation_value, 'platform': 'dfcfw'}


def list_to_linked_list_map(data_list, key) -> LinkedListMap:
    llm = LinkedListMap(key)
    for info in data_list:
        llm.add(Node(info))
    return llm


if __name__ == '__main__':
    print()
